namespace ghost {
	/**怪物角色，编写怪物角色业务*/
	export class Monster extends GameRole{		
		public sleepBtn:SleepBtn;
		public constructor(sceneMgr:SceneManager=null) {
			super(sceneMgr);			
		}	
		protected posChange():void
		{
			super.posChange();
			let s= this;						
			s.updateBodyRange();
		}	
		/**升级*/		
		public levelUp():void
		{
			let s = this;
			let obj:any;
			let roleID:string;			
			let nextLevel:number;
			let levelEffectIDs:string[];
			let effectID:string;
			levelEffectIDs = Main.gameConfig.levelEffectIDs;
			nextLevel = s.level + 1;			
			effectID = levelEffectIDs[nextLevel]?levelEffectIDs[nextLevel]:levelEffectIDs[levelEffectIDs.length-1];
			if(s.isBed())//床升级
			{
				roleID = Main.gameConfig.bedIDs[s.level];
				if(roleID)
				{
					obj = RoleConfig.getRoleConfig(roleID);
					if(obj)
					{
						s.resetSkin(obj.skin);						
						GameRole.roleDataSet(s, obj);						
						s._scene.createEffect(null, RoleType.EFFECT, effectID, s._absX, s._absY, StsType.STAND, 0, map2D.LayerType.TOP);						
					}
				}
			}
			else if(s.isDoor())//门升级
			{
				roleID = Main.gameConfig.doorIDs[s.level];
				if(roleID)
				{
					obj = RoleConfig.getRoleConfig(roleID);
					if(obj)
					{
						s.resetSkin(obj.skin);
						GameRole.roleDataSet(s, obj);						
						s._scene.createEffect(null, RoleType.EFFECT, effectID, s._absX, s._absY, StsType.STAND, 0, map2D.LayerType.TOP);						
					}
				}
			}
			else if(s.isTower())//防御塔升级
			{
				s._scene.createEffect(null, RoleType.EFFECT, effectID, s._absX, s._absY, StsType.STAND, 0, map2D.LayerType.TOP);
				s.cdReduce += (Main.gameConfig.cdReduce?Main.gameConfig.cdReduce:0);
				s.buffRandAdd += (Main.gameConfig.buffRandAdd?Main.gameConfig.buffRandAdd:0);
				++s.level;
				let obj = Main.gameConfig.towerLvSkill;
				if(obj[s._roleID])
				{
					//技能升级
					let skillArr:number[] = obj[s._roleID];
					let ind:number = s.level-1;
					let skillId:number;
					skillId = ind < skillArr.length?skillArr[ind]:skillArr[skillArr.length - 1];
					s.clearSkillList();
					SkillCfg.addDefaultSkill(s, skillId);
				}
				
			}
			s.resetLevelUpCost();
			SoundManager.instance.play(`${Conf.sound}lvup.mp3`, 0, 1,null,null,SoundData.BACKGROUND_SOUND);
		}		
		public resetSkin(skinObj:any)
		{
			super.resetSkin(skinObj);
			let s= this;
			if(s._scene.myPlayer.sts != StsType.FALL && s.sleepBtn == null && s.isBed())
			{
				s.sleepBtn = new SleepBtn;
				s.sleepBtn.scale(0.5,0.5);
				s.sleepBtn.visible = false;
				s.sleepBtn.on(Laya.Event.CLICK, s, ()=>{
					let logic:CommonScene = <CommonScene>s._scene.sceneLogic;		
					logic.myPlayerSleep();
					s.destroySleepBtn();
				});
				s._scene.module.uiLay.addChild(s.sleepBtn);
			}			
		}
		public exitLoop(t: number, index: number): void {
			super.exitLoop(t, index);
			let s= this;
			if(s.sleepBtn)
			{
				let dis:number;
				dis = PositionUtil.calculateDistance(s.absX, s.absY, s._scene.myPlayer.absX, s._scene.myPlayer.absY);
				s.sleepBtn.visible = s._scene.myPlayer.sts != StsType.FALL && dis <= 200 && s.absY > s._scene.myPlayer.absY - 100;
				if(s.sleepBtn.visible && GuideTip.instance.step == 1)
				{
					GuideTip.instance.addGuide(s.sleepBtn);					
				}
			}			
		}
		public worldPosSet(): void {
			super.worldPosSet();
			let s= this;
			if(s.sleepBtn)
			{
				s.sleepBtn.x = s.skin.x - 57;
				s.sleepBtn.y = s.skin.y - 18;
			}
		}
		public destroySleepBtn():void
		{
			let s = this;	
			if(s.sleepBtn)		
			{
				s.sleepBtn.destroy();
				s.sleepBtn = null;
			}
		}
		public inArea(): void {
			let s = this;
			s._scene.sceneLogic.inPos(s);
			s._scene.sceneLogic.inArea(s);			
		}
		public outArea(): void {
			let s= this;
			s._scene.sceneLogic.outPos(s);
			s._scene.sceneLogic.outArea(s,s.col,s.row);						
		}
		protected getSceneScale():number
		{
			return this._scene.monsterScl;
		}
		
		public outPoolInit():void
		{
			super.outPoolInit();			
			let s= this;			
			s.bornTime = Date.now();									
		}		
		public beforeToPool(): void {
			super.beforeToPool();
			let s = this;
			s.destroySleepBtn();
		}
		
	}
}